const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
Page({
  data: {
    globalStype:'',
  	marketGoodsPagedata:[],
    pageNumber:1,
    pageSize:10,
    isEnd:false,
    isloading:true,
    shopId:'',
    marketId:'',
    name:'活动名称',
    backbool:true,
  },
  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
  	if(app.globalData.shopId){
      this.setData({
        shopId:app.globalData.shopId,
      })
    }
    if (option.shopId) {
      this.setData({
        shopId: option.shopId
      })
      app.globalData.shopId = option.shopId
    }
    if(option.id){
      this.setData({
        marketId:option.id
      })
      this.marketGoodsPage()
    }
    if(option.name){
      this.setData({
        name:option.name
      })
    }
    if(getCurrentPages().length<=1){
      this.setData({
        backbool:false
      })
    }else{
      this.setData({
        backbool:true
      })
    }
    if(option.scene){
      let data=decodeURIComponent(option.scene)
      console.log(data)
      let marketId=data.split(",")[0].split(":")[1]
      let shopId=data.split(",")[1].split(":")[1]
      wx.setStorageSync('shopId', shopId)
      this.setData({
        marketId:marketId
      })
      // if(wx.getStorageSync('usertoken')){
        this.marketGoodsPage()
      // }else{
      //   app.GetLogin()
      //   this.marketGoodsPage()
      // }
    }
  },
  onShow(){
    if(this.data.marketGoodsPagedata){

    }else{
      this.marketGoodsPage();
    }
  },
  marketGoodsPage(){
    Controllers.marketGoodsPage({
      pageSize:this.data.pageSize,
      pageNumber:this.data.pageNumber,
      marketId:this.data.marketId
    }).then(res => {
      this.setData({
        marketGoodsPagedata:res.data.list,
        isEnd:res.data.lastPage,
        isloading:false
      })
    });
  },
  onReachBottom() {
    if (this.data.isEnd) {
      return;
    } else {
      // this.setData({
      //     pageNumber: this.data.pageNumber + 1,
      // });
      // this.marketGoodsPage()
      let page=this.data.pageNumber;
      page++;
      this.setData({
        pageNumber:page
      })
      Controllers.marketByGoodsPage({
        pageSize:this.data.pageSize,
        pageNumber:this.data.pageNumber,
        marketId:this.data.marketId
      }).then(res=>{
        let list=this.data.marketGoodsPagedata;
        let list2=res.data.list;
        let cList=list.concat(list2);
        this.setData({
          marketGoodsPagedata:cList,
          isEnd:res.data.lastPage
        })
      })
    }
  },
  onShareAppMessage: function (e) {
      let obj = {
        title: '',
        path:'/pages/Mall/Grouplists?shopId=' + this.data.shopId + '&id=' +this.data.marketId,
        success: function (res) {
          
        },
        fail(err) {
          
        }
      }
      return obj
  },
  onShareTimeline: function (e) {
    let obj = {
      title: '',
      path:'/pages/Mall/Grouplists?shopId=' + this.data.shopId + '&id=' +this.data.marketId,
      success: function (res) {
        
      },
      fail(err) {
        
      }
    }
    return obj
  },
  backindex(e){
    console.log(e)
    if (wx.getStorageSync('usertoken')) {
        wx.navigateTo({
          url:'/pages/Index/Index'
        })
    }else{
      app.GetLogin();
    }
  },

  backIndex(e){
    let page=getCurrentPages()
    let shopId=this.data.shopId
    console.log(page.length)
    if(page.length<=1){
      wx.redirectTo({
        url: '/pages/Index/Index?shopId='+shopId,
      })
    }
  }
})